package serviceswarm

import (
	"context"
	"github.com/aberic/gnomon"
	"github.com/aberic/gnomon/log"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/swarm"
)

// ProjectDeploy ProjectDeploy
func ProjectDeploy(url string, in *swarm.ReqProject) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewProjectServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Deploy(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("project deploy failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "project deploy failed")
}

// ProjectDeployUpdate ProjectDeployUpdate
func ProjectDeployUpdate(url string, in *swarm.ReqProjectUpdate) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewProjectServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Update(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("project deploy failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "project deploy failed")
}

// ProjectDeployRemove 移除服务
func ProjectDeployRemove(url string, in *swarm.ReqProjectRemove) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewProjectServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Remove(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("project deploy failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "project deploy failed")
}

// ProjectServiceState 获取服务状态
func ProjectServiceState(url string, in *swarm.ServiceCompare) (*swarm.ServiceCompare, error) {
	log.Info("User add url request:" + url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewProjectServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.ServiceState(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.ServiceCompare); ok {
			return response, nil
		}
		return nil, errors.New("project service state failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "project service state failed")
}

// ProjectDeployContainer ProjectDeployContainer
func ProjectDeployContainer(url string, in *swarm.ReqProjectContainer) (*swarm.Response, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewProjectServerClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.DeployContainer(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.Response); ok {
			return response, nil
		}
		return nil, errors.New("project deploy failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "project deploy failed")
}
